# 生成器自定义方式
class GetItem:
    def __init__(self, elements):
        self.elements = elements

    def __getitem__(self, index):
        return self.elements[index]


class Generable:
    def __iter__(self):
        return iter([1, 2, 3, 4, 5])


# 什么是可迭代对象
def define_iterable():
    # 1.实现了__iter__对象，返回迭代器,或者__getitem
    # 2.for循环获取数组，iter获取迭代器，next获取数据
    for i in GetItem([1, 3, 5]):
        print(i)

    for i in Generable():
        print(i)


class Generator:
    def __iter__(self):
        return self

    def __next__(self):
        return 1


# 什么是迭代器
def define_iterator():
    # 1.迭代器实现__iter__和__next__
    # 2.迭代器也是可迭代对象
    for i in Generable():
        print(i)


if __name__ == '__main__':
    define_iterable()
    # 使用自定义的 MyList 类
